#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll inf = 1e11;

ll n,s,ans;

ll get(ll x,ll t){
	if(x < t) return x;
	return get(x/t,t) + x % t;
}

int main (){
	scanf("%lld %lld",&n,&s);
	if(n < s) return puts("-1"),0;
	if(n == s) return printf("%lld\n",n+1),0;
	ll b = sqrt(n) + 1;
	for(ll i = 2;i <= b;i++){
		if(get(n,i) == s) {
			printf("%d\n",i);
			return 0;
		}
	}
	ans = inf;
	for(ll i = 1;i * i <= n-s;i++){
		if((n-s) % i == 0){
			b = (n-s) / i + 1;
			if(get(n,b) == s) ans = min(ans,b);
		}
	}
	if(ans == inf) puts("-1");
	else printf("%lld\n",ans);return 0;
}
